﻿#pragma once
#include <iostream>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <vector>
#include <chrono>
#include <list>
class ProcessFramework;
class FrameworkDataCenter {
protected:
    struct ByteData {
        std::wstring data;
        size_t length;
    };

    std::queue<ByteData> byte_data_queue;
    std::mutex queue_mutex;
    std::condition_variable queue_cv;
    std::atomic<bool> stop_thread;
    std::atomic<bool> paused;  // 新增加的暂停标志
    std::thread worker_thread;

    void worker() {
        while (!stop_thread) {
            ByteData byte_data;
            {
                std::unique_lock<std::mutex> lock(queue_mutex);
                if (paused) {  // 如果被暂停了，就等待
                    queue_cv.wait(lock, [this]() { return !paused || stop_thread; });
                }
                else if (queue_cv.wait_for(lock, std::chrono::milliseconds(500), [this]() { return !byte_data_queue.empty() || stop_thread; })) {
                    if (byte_data_queue.empty()) { // 新增加的检查
                        continue;
                    }
                    byte_data = byte_data_queue.front();
                    byte_data_queue.pop();
                }
                else {
                    continue;
                }
            }
            if (!byte_data.data.empty())
            {
                print_byte_data(byte_data.data, byte_data.length);
            }  
        }
    }

    virtual void print_byte_data(std::wstring& data, size_t length) = 0;

public:
    FrameworkDataCenter() {
        stop_thread = true;
        paused = false;  // 初始化为未暂停状态
    }

    virtual ~FrameworkDataCenter() {
        if (stop_thread == false)
        {
            stop_thread = true;
            paused = false;  // 解除暂停状态
            queue_cv.notify_one();
            worker_thread.join();
        }
    }

    void insert_data(std::wstring& data, size_t length) {
        {
            std::lock_guard<std::mutex> lock(queue_mutex);
            byte_data_queue.push({ data, length });
        }
        queue_cv.notify_one();
    }

    void clear_queue() {
        std::lock_guard<std::mutex> lock(queue_mutex);
        std::queue<ByteData>().swap(byte_data_queue);
    }

    void start_worker() {
        if (stop_thread == true)
        {
            stop_thread = false;
            paused = false;  // 解除暂停状态
            worker_thread = std::thread(&FrameworkDataCenter::worker, this);
        }
    }

    void stop_worker() {
        if (stop_thread == false)
        {
            stop_thread = true;
            paused = false;  // 解除暂停状态
            queue_cv.notify_one();
            worker_thread.join();
        }
    }

    void pause_worker() {  // 暂停线程
        paused = true;
    }

    void resume_worker() {  // 恢复线程
        paused = false;
        queue_cv.notify_one();  // 通知线程可以继续工作了
    }
};

class PathQueueSet : public FrameworkDataCenter {
private:
    std::shared_ptr<ProcessFramework> m_fkptr;
protected:
    void print_byte_data(std::wstring& data, size_t length) override;
public:
    void set_fkptr(std::shared_ptr<ProcessFramework>& ptr);
};

class DirQueueSet : public FrameworkDataCenter {
    
private:
    std::shared_ptr<ProcessFramework> m_fkptr;
    void traverseDirectory(const std::wstring& directory, std::list<std::wstring>& filePaths);
protected:
    void print_byte_data(std::wstring& data, size_t length) override;
public:
    void set_fkptr(std::shared_ptr<ProcessFramework>& ptr);
};